package cn.edu.scau.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.edu.scau.bean.ResultBean;
import cn.edu.scau.entity.NursingLevel;
import cn.edu.scau.service.NursingLevelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@Api(tags = "护理等级信息控制器")
@RestController
@RequestMapping("nursinglevel")
public class NursingLevelController {
	@Autowired
	private NursingLevelService nursingLevelService;
	
	/**
	  * 查询所有护理级别信息
	 * @return
	 */
	@ApiOperation(value = "返回所有护理级别信息")
	@GetMapping(value = "/list")
	public ResultBean list(@RequestParam(value = "pageNum",defaultValue = "1")int pageNum,
			@RequestParam(value = "pageSize",defaultValue = "5")int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		List<NursingLevel> nursingLevels=nursingLevelService.selectAll();
		PageInfo<NursingLevel> pageInfo = new PageInfo<NursingLevel>(nursingLevels);
		ResultBean resultBean =new ResultBean(200,true,"查询所有护理级别信息成功~~~",pageInfo);
		return resultBean;
	}
	

	/**
	  * 根据护理级别模糊查询
	 * @param nursinglevellevel
	 * @return
	 */
	@ApiOperation(value = "根据护理级别模糊查询")
	@ApiImplicitParams(value = {
			@ApiImplicitParam(paramType = "query",name="nursinglevellevel",value = "护理级别",dataType = "String"),
	})
	@GetMapping(value = "/find")
	public ResultBean find(String nursinglevellevel,
			@RequestParam(value = "pageNum",defaultValue = "1")int pageNum,
			@RequestParam(value = "pageSize",defaultValue = "5")int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		List<NursingLevel> nursingLevels=nursingLevelService.findByNursinglevellevel(nursinglevellevel);
		PageInfo<NursingLevel> pageInfo = new PageInfo<NursingLevel>(nursingLevels);
		ResultBean r =new ResultBean(200,true,"模糊查询成功~~~",pageInfo);
		return r;
	}
	
	
	/**
	  * 新增护理级别
	 * @param nursingLevel
	 * @param bindingResult
	 * @return
	 */
	@PostMapping(value = "/insert")
	public ResultBean insert(@Validated NursingLevel nursingLevel,BindingResult bindingResult)  {
		ResultBean resultBean =null;
		if(bindingResult.hasErrors()) {
			//方法一：将无法通过数据校验的信息，合并成一个字符串，返回给前端
			StringBuffer msg = new StringBuffer();
			for(FieldError f: bindingResult.getFieldErrors()) {
				msg.append(f.getField()+"："+f.getDefaultMessage()+"\n");
			}
			resultBean=new ResultBean(5005,false,msg.toString(),null);
			//方法二：将所有无法通过数据校验的信息集合，合并成一个字符串，返回给前端
//			resultBean=new ResultBean(5005,false,"实体对象赋值异常",null);
			return resultBean;
		}
		boolean flag = nursingLevelService.insert(nursingLevel);
		if(flag) {
			resultBean = new ResultBean(200,true,"新增护理级别成功~~~\n",null);
		}else {
			resultBean = new ResultBean(5005,false,"新增护理级别失败",null);
		}
		return resultBean;
	}
	
	/**
	  * 显示详细的护理级别信息
	 * @param nursinglevelid
	 * @return
	 */
	@ApiOperation(value = "根据护理级别编号查询信息")
	@ApiImplicitParam(paramType = "path",name = "nursinglevelid",value = "编号",required = true,dataType = "int")
	@GetMapping(value = "/{nursinglevelid}")
	public ResultBean detail(@PathVariable("nursinglevelid")Integer nursinglevelid) {
		NursingLevel nursingLevel = nursingLevelService.selectById(nursinglevelid);
		ResultBean resultBean=new ResultBean(200,true,"查询成功~~~",nursingLevel);
		return resultBean;
	}
	

	/**
	  * 修改护理级别
	 * @param nursinglevelid
	 * @param nursingLevel
	 * @param bindingResult
	 * @return
	 */
	@PutMapping(value = "/update/{nursinglevelid}")
	public ResultBean update(@PathVariable("nursinglevelid")Integer nursinglevelid,@Validated NursingLevel nursingLevel,BindingResult bindingResult) {
		System.err.println(nursingLevel);
		ResultBean resultBean =null;
		if(bindingResult.hasErrors()) {
			//方法一：将无法通过数据校验的信息，合并成一个字符串，返回给前端
			StringBuffer msg = new StringBuffer();
			for(FieldError f: bindingResult.getFieldErrors()) {
				msg.append(f.getField()+"："+f.getDefaultMessage()+"\n");
			}
			resultBean=new ResultBean(5005,false,msg.toString(),null);
			//方法二：将所有无法通过数据校验的信息集合，合并成一个字符串，返回给前端
//			resultBean=new ResultBean(5005,false,"实体对象赋值异常",null);
			return resultBean;
		}

		boolean flag = nursingLevelService.update(nursingLevel);
		if(flag) {
			resultBean = new ResultBean(200,true,"修改护理级别成功~~~\n修改护理级别编号为"+nursingLevel.getNursinglevelid(),null);
		}else {
			resultBean = new ResultBean(5005,false,"修改护理级别失败",null);
		}
		return resultBean;
	}
	
	/**
	  * 删除护理级别
	 * @param nursinglevelid
	 * @return
	 */
	@DeleteMapping(value = "/delete/{nursinglevelid}")
//	@ResponseBody
	public ResultBean delete(@PathVariable("nursinglevelid")Integer nursinglevelid) {
		boolean flag = nursingLevelService.delete(nursinglevelid);
		ResultBean resultBean =null;
		if(flag) {
			resultBean = new ResultBean(200,true,"删除护理级别成功~~~\n删除护理级别编号为"+nursinglevelid,null);
		}else {
			resultBean = new ResultBean(5005,false,"删除护理级别失败",null);
		}
		return resultBean;
	}
}
